package com.ayoka.parenting.ui.meditation.more;

import com.ayoka.parenting.R;
import com.ayoka.parenting.bean.MeditationGroupBean;
import com.ayoka.parenting.bean.MeditationMoreDataBean;
import com.ayoka.parenting.bean.MeditationMoreMultiEntity;
import com.ayoka.parenting.interf.SimpleRequestCallback;
import com.ayoka.parenting.interf.SimpleRequestCallback;
import com.ayoka.parenting.mvp.BasePresenterImpl;
import com.jincp.common.utils.UIUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @ProjectName: MVPPlugin.
 * @ClassName: MeditationMorePresenter.
 * @Description: .
 * @Author: jincp.
 * @CreateDate: 2019/10/10 18:17.
 */
public class MeditationMorePresenter extends BasePresenterImpl<MeditationMoreContract.View> implements MeditationMoreContract.Presenter {

    private MeditationMoreModel model;

    public MeditationMorePresenter() {
        model = new MeditationMoreModel();
    }

    @Override
    public void getMeditationMoreListData(boolean isShowWaitDialog) {
        if (isShowWaitDialog) {
            mView.get().showWaitDialog(UIUtils.getString(R.string.loading));
        }
        model.getMeditationMoreListData(this, new SimpleRequestCallback<MeditationMoreDataBean>() {
            @Override
            public void onSuccess(MeditationMoreDataBean data) {
                if (!isAttached()) return;
                mView.get().hideWaitDialog();
                List<MeditationMoreMultiEntity> listData = formatMeditationMoreMultiEntityListData(data);
                mView.get().configMeditationMoreListData(data.last_meditation_id, listData);
            }

            @Override
            public void onFailure(int errCode, String errMsg) {
                if (!isAttached()) return;
                mView.get().hideWaitDialog();
                mView.get().configMeditationMoreListData(null, null);
            }
        });
    }


    private List<MeditationMoreMultiEntity> formatMeditationMoreMultiEntityListData(MeditationMoreDataBean data) {
        List<MeditationMoreMultiEntity> result = new ArrayList<>();
        for (MeditationGroupBean meditationGroupBean : data.list) {
            result.add(new MeditationMoreMultiEntity(MeditationMoreMultiEntity.TITLE, meditationGroupBean));
            result.add(new MeditationMoreMultiEntity(MeditationMoreMultiEntity.ITEM, meditationGroupBean.Meditations));
        }
        return result;
    }
}
